package Array;

public class item42 {
    public int trap(int[] height) {
        int sum = 0;
        for (int i = 0; i < height.length; i++) {
            int h = height[i];
           int toLeft = i,toRight=i;
           int maxLeft=0,maxRight = 0;
           while (toLeft>=0)
           {
               maxLeft = Math.max(maxLeft,height[toLeft]);
               toLeft--;
           }
           while (toRight<height.length)
           {
               maxRight = Math.max(maxRight,height[toRight]);
           }
           sum=sum+Math.min(maxLeft,maxRight)-height[i];
        }
    return sum;
    }
}
